package com.dayu.gulimall.coupon.service.impl;

import com.dayu.common.to.SkuReductionTo;
import com.dayu.gulimall.coupon.entity.MemberPriceEntity;
import com.dayu.gulimall.coupon.entity.SkuLadderEntity;
import com.dayu.gulimall.coupon.service.MemberPriceService;
import com.dayu.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayu.common.utils.PageUtils;
import com.dayu.common.utils.Query;

import com.dayu.gulimall.coupon.dao.SkuFullReductionDao;
import com.dayu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.dayu.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存商品满减优惠信息
     *
     * @param to
     */
    @Override
    public void saveSkuReductionTo(SkuReductionTo to) {
//        保存商品满减，打折信息

        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(to.getSkuId());
        skuLadderEntity.setFullCount(to.getFullCount());
        skuLadderEntity.setDiscount(to.getDiscount());
        skuLadderEntity.setAddOther(to.getCountStatus());
        if (to.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        //2.保存满减信息
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(to, skuFullReductionEntity);
        //如果满减价格大于0再保存
        if (to.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            this.save(skuFullReductionEntity);
        }

        //3、保存会员价格
        List<MemberPriceEntity> collect = to.getMemberPrice().stream()
                .map(item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(to.getSkuId());
                    memberPriceEntity.setMemberLevelId(item.getId());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setAddOther(1);//是否叠加 优惠，默认都叠加
                    return memberPriceEntity;
                })
                .filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1)
                .collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}